package mypackage;

import mypackage.dao.ListDao;
import net.rim.device.api.system.Characters;
import net.rim.device.api.system.KeyListener;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.component.table.SimpleList;
import net.rim.device.api.ui.container.MainScreen;

class ListScreen extends MainScreen {
	ListDao listDao;
	SimpleList simpleList;
	int index = -1;

	public ListScreen(ListDao listDao) {

		super(Manager.NO_VERTICAL_SCROLL);
		this.listDao = listDao;

		String tmp = listDao == null ? "list" : "item";
		addNewListMenuItem = new MenuItem("New" + tmp, 110, 1) {
			public void run() {
				performAddOp();
			}
		};
		deleteListMenuItem = new MenuItem("Delete" + tmp, 110, 1) {
			public void run() {
				performDelOp();
			}
		};
		addKeyListener(new KeyListener() {

			public boolean keyUp(int keycode, int time) {
				// TODO Auto-generated method stub
				return false;
			}

			public boolean keyStatus(int keycode, int time) {
				// TODO Auto-generated method stub
				return false;
			}

			public boolean keyRepeat(int keycode, int time) {
				// TODO Auto-generated method stub
				return false;
			}

			public boolean keyDown(int keycode, int time) {
				// TODO Auto-generated method stub
				return false;
			}

			public boolean keyChar(char key, int status, int time) {
				if (key == 'n') {
					performAddOp();
				} else if (key == 'd' || key == Characters.BACKSPACE) {
					performDelOp();
				}

				return false;
			}
		});

		setTitle(listDao == null ? "Lists" : listDao.listName);
		addMenuItem(addNewListMenuItem);
		addMenuItem(deleteListMenuItem);

		Manager manager = getMainManager();
		simpleList = new SimpleList(manager);

		// simpleList.setCommand(new CommandHandler() {
		//
		// public void execute(ReadOnlyCommandMetadata metadata, Object context)
		// {
		// if (ListScreen.this.listDao == null) {
		// ((HelloBlackBerry) getApplication())
		// .openListWithIndex(simpleList.getFocusRow());
		// }
		// }
		// }, null, null);
		// Vector items = listDao == null ? ((HelloBlackBerry)
		// getApplication()).dbObject
		// .getListNames() : listDao.listItems;
		// for (int i = 0; i < items.size(); i++)
		// simpleList.add(items.elementAt(i).toString());

	}

	MenuItem addNewListMenuItem;

	MenuItem deleteListMenuItem;

	public void addItem(String arg) {
		simpleList.add(arg);
	}

	public void performAddOp() {
		HelloBlackBerry berry = (HelloBlackBerry) getApplication();
		berry.pushScreen(new AddItemScreen(berry.selectedIndex));
	}

	public void performDelOp() {
		if (simpleList.getFocusRow() >= 0) {
			simpleList.remove(simpleList.getFocusRow());
			// if (listDao == null)
			// ((HelloBlackBerry) getApplication()).deleteList(simpleList
			// .getFocusRow());
			// else
			// ((HelloBlackBerry) getApplication()).deleteListItem(simpleList
			// .getFocusRow());
		}
	}

	protected void onExposed() {
		// TODO Auto-generated method stub
		super.onExposed();
		if (listDao == null) {
			((HelloBlackBerry) getApplication()).selectedIndex = -1;
		}
	}
}